/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.bangong.swclxxl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.modules.bangong.swclxxl.domain.Swcljbqk;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.bangong.swclxxl.service.SwcljbqkService;
import me.zhengjie.modules.bangong.swclxxl.domain.dto.SwcljbqkQueryCriteria;
import me.zhengjie.modules.bangong.swclxxl.mapper.SwcljbqkMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author Houger
* @date 2025-03-09
**/
@Service
@RequiredArgsConstructor
public class SwcljbqkServiceImpl extends ServiceImpl<SwcljbqkMapper, Swcljbqk> implements SwcljbqkService {

    private final SwcljbqkMapper swcljbqkMapper;

    @Override
    public PageResult<Swcljbqk> queryAll(SwcljbqkQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(swcljbqkMapper.findAll(criteria, page));
    }

    @Override
    public List<Swcljbqk> queryAll(SwcljbqkQueryCriteria criteria){
        return swcljbqkMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Swcljbqk resources) {
        swcljbqkMapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Swcljbqk resources) {
        Swcljbqk swcljbqk = getById(resources.getColumnId());
        swcljbqk.copy(resources);
        swcljbqkMapper.update(swcljbqk,new QueryWrapper<>());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        swcljbqkMapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<Swcljbqk> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Swcljbqk swcljbqk : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("签收人职工号", swcljbqk.getQsrzgh());
            map.put("拆封人职工号", swcljbqk.getCfrzgh());
            map.put("登记人职工号", swcljbqk.getDjrzgh());
            map.put("收文日期", swcljbqk.getSwrq());
            map.put("来文单位", swcljbqk.getLwdw());
            map.put("份数", swcljbqk.getFs());
            map.put("处理情况", swcljbqk.getClqk());
            map.put("翻印份数", swcljbqk.getFyfs());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}